<?php
/**
 * +--------------------------------------------------------+
 * @Created by Phpstorm.
 * @Category ExceptionErrorRepository.php
 * @Depiction
 * @Author hwy <429208948@qq.com>
 * @Date 2022/3/23  16:57
 * +--------------------------------------------------------+
 */

namespace App\Repositories;

use App\Models\ExceptionError;

class ExceptionErrorRepository extends BaseRepository
{
    protected $exceptionError;

    public function __construct(ExceptionError $exceptionError)
    {
        $this->exceptionError = $exceptionError;
    }

    /**
     * @param $params
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     * @Depiction 获取列表
     * @Author hwy <429208948@qq.com>
     * @Date 2022/3/23  19:48
     */
    public function getList($params)
    {
        $query = $this->exceptionError->query();

        $perPage = $this->perPage($params);

        [$sort, $order] = $this->paramsSet($params);

        $query = $query->select('id', 'message', 'code', 'file', 'line', 'trace', 'trace_as_string', 'is_solve', 'created_at', 'updated_at');

        //
        if (!empty($params['is_solve'])) {
            $query = $query->where('is_solve', '=', $params['is_solve']);
        }

        if (!empty($params['id'])) {
            $query = $query->where('id', 'like', '%' . $params['id'] . '%');
        }

        if (!empty($params['message'])) {
            $query = $query->where('message', 'like', '%' . $params['message'] . '%');
        }

        if (!empty($params['start_at'])) {
            $query = $query->whereBetween('created_at', [$params['start_at'], $params['end_at']]);
        }

        $list = $query->orderBy($sort, $order)->paginate($perPage);

        return $list;
    }

    /**
     * @param string $id id
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     * @Depiction 获取指定id信息
     * @Author hwy <429208948@qq.com>
     * @Date 2022/3/23  20:14
     */
    public function getInfo(string $id)
    {
        return $data = $this->exceptionError->query()
            ->where('id', $id)
            ->first();
    }

    /**
     * @param $exception
     * @return bool
     * @Depiction 修正错误信息
     * @Author hwy <429208948@qq.com>
     * @Date 2022/3/23  20:21
     */
    public function solve($exception)
    {
        $exception->is_solve = 1;

        $solve = $exception->save();

        return $solve;

    }

}
